package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.washe.config.*;
import com.yanqu.road.entity.activity.washe.data.*;
import com.yanqu.road.entity.enums.eLoginLinkType;
import com.yanqu.road.entity.enums.washe.eWasheDecorationType;
import com.yanqu.road.entity.enums.washe.eWasheFunction;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WashePb {

    public static WasheProto.WasheSyncConfigRespMsg.Builder parseWasheSyncConfigRespMsg(WasheConfig config, String language) {
        WasheProto.WasheSyncConfigRespMsg.Builder configBuilder = WasheProto.WasheSyncConfigRespMsg.newBuilder();
        for (WasheCardConfig cardConfig : config.getCardConfigMap().values()) {
            configBuilder.addCardConfig(parseWasheCardConfigMsg(cardConfig, language));
        }
        for (WasheSceneConfig sceneConfig : config.getSceneConfigMap().values()) {
            if (sceneConfig.isOpen()) { // 只下发开启的机制
                configBuilder.addSceneConfig(parseWasheSceneConfigMsg(sceneConfig, language));
            }
        }
        for (WasheModeConfig modeConfig : config.getModeConfigMap().values()) {
            configBuilder.addModeConfig(parseWasheModeConfigMsg(modeConfig));
        }
        for (List<WasheBoxConfig> list : config.getBoxConfigListMap().values()) {
            for (WasheBoxConfig boxConfig : list) {
                configBuilder.addBoxConfig(parseWasheBoxConfigMsg(boxConfig));
            }
        }
        for (WasheRankConfig rankConfig : config.getRankConfigMap().values()) {
            configBuilder.addRankConfig(parseWasheRankConfigMsg(rankConfig));
        }
        for (WasheRoundConfig roundConfig : config.getRoundConfigMap().values()) {
            configBuilder.addRoundConfig(parseWasheRoundConfigMsg(roundConfig));
        }

        // 这个配置比较特殊
        addSeasonTimeConfig(config, configBuilder);

        for (List<WasheHonorMomentConfig> list : config.getHonorMomentConfigMap().values()) {
            if (list.isEmpty()) {
                continue;
            }
            configBuilder.addHonorMomentConfig(parseWasheHonorMomentConfigMsg(list, language));
        }
        for (WasheSeasonConfig seasonConfig : config.getSeasonConfigMap().values()) {
            configBuilder.addSeasonConfig(parseWasheSeasonConfig(seasonConfig, language));
        }
        for (WasheSkillConfig skillConfig : config.getSkillConfigMap().values()) {
            if (skillConfig.isOpen()) { // 只下发开启的技能
                configBuilder.addSkillConfig(parseWasheSkillConfigMsg(skillConfig, language));
            }
        }
        return configBuilder;
    }

    private static void addSeasonTimeConfig(WasheConfig config, WasheProto.WasheSyncConfigRespMsg.Builder configBuilder) {
        Map<Integer, WasheSeasonConfig> seasonConfigMap = config.getSeasonConfigMap();
        if (seasonConfigMap.isEmpty()) {
            return; // 没有任何赛季整个都不传
        }

        long now = System.currentTimeMillis();
        WasheSeasonConfig lastOrCur = null;
        WasheSeasonConfig next = null;
        for (WasheSeasonConfig seasonConfig : seasonConfigMap.values()) {

            if (seasonConfig.getBeginShowTime() < now) {
                if (lastOrCur == null || lastOrCur.getEndShowTime() < seasonConfig.getEndShowTime()) {
                    lastOrCur = seasonConfig;
                }
            }

            if (seasonConfig.getBeginShowTime() > now) {
                if (next == null || seasonConfig.getBeginShowTime() < next.getBeginShowTime()) {
                    next = seasonConfig;
                }
            }

        }

        WasheProto.WasheSeasonTimeConfigMsg.Builder builder = WasheProto.WasheSeasonTimeConfigMsg.newBuilder();
        if (lastOrCur != null) {
            builder.setSeasonId(lastOrCur.getSeasonId());
            builder.setBeginShowTime(lastOrCur.getBeginShowTime());
            builder.setBeginTime(lastOrCur.getBeginTime());
            builder.setEndTime(lastOrCur.getEndTime());
            builder.setEndShowTime(lastOrCur.getEndShowTime());
            builder.setTaskItemId(lastOrCur.getTaskItemId());
            builder.setIntegralBeginTime(lastOrCur.getIntegralBeginTime());
            builder.setIntegralEndTime(lastOrCur.getIntegralEndTime());
            builder.setPeakednessBeginTime(lastOrCur.getPeakednessBeginTime());
            builder.setPeakednessEndTime(lastOrCur.getPeakednessEndTime());
            builder.setSwitch(lastOrCur.getJinBiaoSwitch());
        } else {
            builder.setSeasonId(-1);
            builder.setBeginShowTime(0);
            builder.setBeginTime(0);
            builder.setEndTime(0);
            builder.setEndShowTime(0);
            builder.setTaskItemId(0);
            builder.setIntegralBeginTime(0);
            builder.setIntegralEndTime(0);
            builder.setPeakednessBeginTime(0);
            builder.setPeakednessEndTime(0);
            builder.setSwitch(0);
        }

        if (next != null) {
            builder.setNextSeasonTime(next.getBeginShowTime());
        } else {
            builder.setNextSeasonTime(0);
        }

        // 设置值
        configBuilder.setSeasonTimeConfig(builder);
    }

    private static WasheProto.WasheSeasonConfig.Builder parseWasheSeasonConfig(WasheSeasonConfig seasonConfig, String language) {
        WasheProto.WasheSeasonConfig.Builder builder = WasheProto.WasheSeasonConfig.newBuilder();
        builder.setSeasonId(seasonConfig.getSeasonId());
        builder.setTheme(ServerLanguageMgr.getContent(seasonConfig.getTheme(), language));
        builder.setDesc(ServerLanguageMgr.getContent(seasonConfig.getThemeDesc(), language));
        return builder;
    }

    private static WasheProto.WasheHonorMomentConfigMsg.Builder parseWasheHonorMomentConfigMsg(List<WasheHonorMomentConfig> list, String language) {
        WasheProto.WasheHonorMomentConfigMsg.Builder builder = WasheProto.WasheHonorMomentConfigMsg.newBuilder();
        WasheHonorMomentConfig first = list.get(0);
        builder.setType(first.getHonorMomentType());
        builder.setTitle(ServerLanguageMgr.getContent(first.getTitle(), language));
        builder.setDesc(ServerLanguageMgr.getContent(first.getDesc(), language));

        for (WasheHonorMomentConfig honorMomentConfig : list) {
            WasheProto.WasheHonorMomentTempConfigMsg.Builder tmp = WasheProto.WasheHonorMomentTempConfigMsg.newBuilder();
            tmp.setId(honorMomentConfig.getHonorMomentId());
            tmp.setCondition(honorMomentConfig.getCondition());
            builder.addList(tmp);
        }
        return builder;
    }

    public static WasheProto.WasheBoxDataMsg.Builder parseWasheBoxDataMsg(int index, WasheBoxData data) {
        WasheProto.WasheBoxDataMsg.Builder builder = WasheProto.WasheBoxDataMsg.newBuilder();
        builder.setIndex(index);
        builder.setBoxId(data.getBoxId());
        builder.setCanOpenTime(data.getCanOpenTime());
        return builder;
    }

    public static WasheProto.WasheUserDataMsg.Builder parseWasheUserDataMsg(WasheConfig config, WasheUserData data, WasheUserJinBiaoData jinBiaoData, WasheProto.WasheGameOverMsg gameOverMsg) {
        WasheProto.WasheUserDataMsg.Builder builder = WasheProto.WasheUserDataMsg.newBuilder();
        builder.addAllUnlockCard(new ArrayList<>(data.getUnlockCard()));
        builder.setScore(data.getScore());
        if (data.getSilverSet().contains(config.getSeasonId())) {
            builder.addZhanLing(1);
        }
        if (data.getGoldSet().contains(config.getSeasonId())) {
            builder.addZhanLing(2);
        }
        List<WasheBoxData> boxDataList = data.getBoxDataList();
        for (int i = 0; i < boxDataList.size(); i++) {
            builder.addBox(parseWasheBoxDataMsg(i, boxDataList.get(i)));
        }
        builder.setBuffTimes(data.getBuffTimes());
        builder.setDataSeason(data.getDataSeason());
        builder.setBuffCollectTimes(eWasheFunction.ContinueWinBuff.isOpen(config.getVersion()) ? data.getContinueWinTimes() : data.getBuffCollectTimes());
        builder.setGuide(data.isGuide());
        if (gameOverMsg != null) {
            builder.setOverMsg(gameOverMsg);
        }
        for (WasheRmbMallData mallData : data.getRmbMallMap().values()) {
            if (mallData.getBeginTime() == 0 && mallData.getTimes() == 0) {
                continue;
            }
            builder.addMall(WasheProto.WasheMallMsg.newBuilder()
                    .setMallId(mallData.getMallId())
                    .setBeginTime(mallData.getBeginTime())
                    .setCanBuyTimes(mallData.getTimes())
            );
        }
        builder.setGuide2(data.getPlayTimes() == 0);
        builder.setUnlockSceneMode(eWasheFunction.PanMianJiZhi.isOpen(config.getVersion()) && data.getScore() >= config.getUnlockSceneScore()); // 开启盘面机制 && 分数达到解锁条件
        builder.addAllTodaySceneMode(WasheMgr.getSceneList());
        builder.setDayPlayTimes(data.getDayPlayTimes());
        builder.setCanSurrenderTimes(eWasheFunction.ContinueWinBuff.isOpen(config.getVersion()) ? (config.getDaySurrenderTimes() - data.getDaySurrenderTimes()) : 1);
        if (jinBiaoData != null) {
            builder.setJinBiaoUserData(parseWasheUserJinBiaoDataMsg(jinBiaoData));
        }
        builder.addAllUnlockSkill(eWasheFunction.ChangeSkill.isOpen(config.getVersion()) ? new ArrayList<>(data.getUnlockSkill()) : new ArrayList<>());
        builder.addAllSelectedSkill(data.getSelectedSkill());
        builder.setShareRewardTime(data.getShareRewardTime());
        builder.setLinkGuide(data.isLinkGuide());
        Map<Integer, Integer> decorationMap = data.getDecorationMap();
        if (decorationMap == null) {
            decorationMap = new ConcurrentHashMap<>();
        }
        builder.setUseBoardSkin(decorationMap.getOrDefault(eWasheDecorationType.BoardSkin.getValue(), 0));
        builder.setUseAnimation(decorationMap.getOrDefault(eWasheDecorationType.Animation.getValue(), 0));
        builder.setUseEffect(decorationMap.getOrDefault(eWasheDecorationType.Effect.getValue(), 0));

        return builder;
    }

    public static WasheProto.WasheUserJinBiaoDataMsg.Builder parseWasheUserJinBiaoDataMsg(WasheUserJinBiaoData jinBiaoData) {
        WasheProto.WasheUserJinBiaoDataMsg.Builder builder = WasheProto.WasheUserJinBiaoDataMsg.newBuilder();
        builder.setScore(jinBiaoData.getScore());
        builder.setEnergy(jinBiaoData.getEnergy());
        builder.setContinueWinTimes(jinBiaoData.getContinueWinTimes());
        builder.setPeakWinTimes(jinBiaoData.getPeakWinTimes());
        builder.setPeakScore(jinBiaoData.getPeakScore());
        return builder;
    }

    public static WasheProto.WasheSkillConfigMsg.Builder parseWasheSkillConfigMsg(WasheSkillConfig skillConfig, String language) {
        WasheProto.WasheSkillConfigMsg.Builder builder = WasheProto.WasheSkillConfigMsg.newBuilder();
        builder.setSkillId(skillConfig.getSkillId());
        for (Map.Entry<Integer, Long> entry : skillConfig.getPriceMap().entrySet()) {
            builder.addPrice(WasheProto.WasheSkillPrice.newBuilder()
                    .setNum(entry.getKey())
                    .setPrice(entry.getValue().intValue())
            );
        }
        builder.setSkillParam(skillConfig.getSkillParam());
        builder.setSkillName(ServerLanguageMgr.getContent(skillConfig.getSkillName(), language));
        builder.setSkillDesc(ServerLanguageMgr.getContent(skillConfig.getSkillDesc(), language));
        builder.setItemId(skillConfig.getItemId());
        return builder;
    }

    public static WasheProto.WasheCardConfigMsg.Builder parseWasheCardConfigMsg(WasheCardConfig config, String language) {
        WasheProto.WasheCardConfigMsg.Builder builder = WasheProto.WasheCardConfigMsg.newBuilder();
        builder.setCardId(config.getCardId());
        builder.setQuality(config.getQuality());
        builder.setPrice((int) config.getPrice());
        builder.setSkillParam(config.getSkillParam());
        builder.setCd(config.getCd());
        builder.setPic(config.getPic());
        builder.setCardName(ServerLanguageMgr.getContent(config.getCardName(), language));
        builder.setCardDesc(ServerLanguageMgr.getContent(config.getCardDesc(), language));
        builder.setItemId(config.getItemId());
        return builder;
    }

    public static WasheProto.WasheSceneConfigMsg.Builder parseWasheSceneConfigMsg(WasheSceneConfig config, String language) {
        WasheProto.WasheSceneConfigMsg.Builder builder = WasheProto.WasheSceneConfigMsg.newBuilder();
        builder.setSceneId(config.getSceneId());
        builder.setParam(config.getParam());
        builder.setSceneName(ServerLanguageMgr.getContent(config.getSceneName(), language));
        builder.setSceneDesc(ServerLanguageMgr.getContent(config.getSceneDesc(), language));
        return builder;
    }

    public static WasheProto.WasheModeConfigMsg.Builder parseWasheModeConfigMsg(WasheModeConfig config) {
        WasheProto.WasheModeConfigMsg.Builder builder = WasheProto.WasheModeConfigMsg.newBuilder();
        builder.setModeId(config.getModeId());
        builder.setMaxRound(config.getMaxRound());
        builder.setMaxStep(config.getMaxStep());
        builder.setClearTime((int) (config.getClearTime() / 1000));
        builder.setExParam(config.getExParam());
        return builder;
    }

    public static WasheProto.WasheRankConfigMsg.Builder parseWasheRankConfigMsg(WasheRankConfig config) {
        WasheProto.WasheRankConfigMsg.Builder builder = WasheProto.WasheRankConfigMsg.newBuilder();
        builder.setSeasonId(config.getSeasonId());
        builder.setRankId(config.getRankId());
        builder.setRankScore(config.getRankScore());
        builder.setEndReset(config.getEndReset());
        builder.setShowSkin(config.getShowSkin());
        builder.setMedal(config.getMedal());
        builder.setWinPoint((int) config.getWinPoint());
        builder.setLosePoint((int) config.getLosePoint());
        return builder;
    }

    public static WasheProto.WasheRoundConfigMsg.Builder parseWasheRoundConfigMsg(WasheRoundConfig config) {
        WasheProto.WasheRoundConfigMsg.Builder builder = WasheProto.WasheRoundConfigMsg.newBuilder();
        builder.setSeasonId(config.getSeasonId());
        builder.setRound(config.getRound());
        builder.setSceneSkillId(config.getSceneSkillId());
        builder.setWarTime(config.getWarTime());
        builder.setPreparationTime(config.getPreparationTime());
        return builder;
    }

    public static WasheProto.WasheBoxConfigMsg.Builder parseWasheBoxConfigMsg(WasheBoxConfig config) {
        WasheProto.WasheBoxConfigMsg.Builder builder = WasheProto.WasheBoxConfigMsg.newBuilder();
        builder.setBoxId(config.getBoxId());
        builder.setSeasonId(config.getSeasonId());
        builder.setQuality(config.getQuality());
        builder.setReward("");
        return builder;
    }

    public static WasheBattleRecordOpponent parseWasheBattleRecordOpponent(WasheProto.WasheOpponentMsg msg) {
        WasheBattleRecordOpponent data = new WasheBattleRecordOpponent();
        data.setScore(msg.getScore());
        data.setUserId(msg.getUserId());
        data.setInfo(PlayerBasePb.parseBaseUserInfo(msg.getPlayerInfo()));
        data.setChannelId(msg.getChannelId());
        data.setServerName(msg.getServerName());
        return data;
    }

    public static WasheProto.WasheOpponentMsg.Builder parseWasheOpponentMsg(WasheBattleRecordOpponent data) {
        WasheProto.WasheOpponentMsg.Builder builder = WasheProto.WasheOpponentMsg.newBuilder();
        builder.setScore(data.getScore());
        builder.setUserId(data.getUserId());
        builder.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(data.getInfo()));
        builder.setChannelId(data.getChannelId());
        builder.setServerName(data.getServerName());
        return builder;
    }

    public static WasheProto.WasheBattleRecordMsg.Builder parseWasheBattleRecordMsg(WasheBattleRecord data) {
        WasheProto.WasheBattleRecordMsg.Builder builder = WasheProto.WasheBattleRecordMsg.newBuilder();
        builder.setNo(data.getNo());
        builder.setTime(data.getTime());
        builder.setOpponent(parseWasheOpponentMsg(data.getOpponentInfo()));
        builder.setOpponentCard(data.getOpponentCardId());
        builder.setOpponentScore(data.getOpponentScore());
        builder.setSelfCard(data.getSelfCardId());
        builder.setSelfScore(data.getSelfScore());
        builder.setWin(data.isWin());
        builder.setScoreChange(data.getScoreChange());
        builder.addAllSelfSkillId(data.getSelfSkills());
        builder.addAllOpponentSkillId(data.getOpponentSkills());
        builder.setJinBiaoScoreChange(data.getJinBiaoScoreChange());
        builder.setType(data.getType());
        return builder;
    }

    public static WasheProto.WasheBadgeMsg.Builder parseWasheBadgeMsg(WasheTmpBadge badge) {
        WasheProto.WasheBadgeMsg.Builder builder = WasheProto.WasheBadgeMsg.newBuilder();
        builder.setBadgeId(badge.getBadgeId());
        builder.setScore(badge.getScore());
        builder.setSeasonId(badge.getSeasonId());
        Long jinBiaoScoreCanNull = badge.getJinBiaoScoreCanNull();
        if (jinBiaoScoreCanNull != null) {
            builder.setJinBiaoScore(jinBiaoScoreCanNull);
        }
        return builder;
    }

    public static WasheProto.WasheHonorMomentMsg.Builder parseWasheHonorMomentMsg(WasheHonorMomentTemp data) {
        WasheProto.WasheHonorMomentMsg.Builder builder = WasheProto.WasheHonorMomentMsg.newBuilder();
        builder.setHonorMomentId(data.getId());
        builder.setTime(data.getTime());
        return builder;
    }
}
